//
//  ViewController.swift
//  cityList
//
//  Created by ning on 2018/1/26.
//  Copyright © 2018年 ning. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
   
    @IBOutlet weak var tableView: UITableView!
    lazy var letterLabel: UILabel = {
        let label = UILabel()
        label.frame = CGRect(x:UIScreen.main.bounds.width - 15,y:(UIScreen.main.bounds.height - 380) / 2,width:13,height:380)
        label.numberOfLines = 0
        label.font = UIFont.systemFont(ofSize: 12)
        label.backgroundColor = UIColor.lightGray
        label.isUserInteractionEnabled = true
        label.layer.cornerRadius = 5
        label.layer.masksToBounds = true
        label.alpha = 0.7
        label.textAlignment = .center
        return label
    }()
    
    lazy var indexLabel: UILabel = {
        let label = UILabel()
        label.frame = CGRect(x:0,y:0,width:100,height:100)
        label.numberOfLines = 0
        label.font = UIFont.systemFont(ofSize: 30)
        label.backgroundColor = UIColor.red
        label.layer.cornerRadius = label.frame.width / 2
        label.layer.masksToBounds = true
        label.alpha = 0
        label.textAlignment = .center
        label.textColor = .white
        return label
    }()
    var letterArr = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
      
        setupLetterLabel()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

extension ViewController {
    func setupTableView() {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
    }
    
    func setupLetterLabel() {
        view.addSubview(letterLabel)
        view.addSubview(indexLabel)
        indexLabel.center = view.center
        for i in 0..<26 {
            let str = NSString(format: "%c", i + 65)
            print(str)
            letterArr.append(str as String)
            letterLabel.text = i == 0 ? letterArr[i] : (letterLabel.text ?? "") + "\n" + letterArr[i]
        }
          setupTableView()
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       didTouchAction(touches)
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        didTouchAction(touches)
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        UIView.animate(withDuration: 0.5) {
            self.indexLabel.alpha = 0
        }
    }

    func didTouchAction(_ touches: Set<UITouch>) {
        UIView.animate(withDuration: 0.5) {
            self.indexLabel.alpha = 1
        }
        let touch = touches.first
        let point = touch?.location(in: letterLabel)
        let index = Int((point?.y ?? 0.0) / letterLabel.bounds.height * 26)
        if index > 25 || index < 0 {
            return
        }
        indexLabel.text = letterArr[index]

        let indexPath = IndexPath(row: 0, section: index)
        tableView.scrollToRow(at: indexPath, at: .top, animated: false)
    }
}


extension ViewController: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return letterArr.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        cell?.textLabel?.text = "第\(indexPath.section)" + "\(indexPath.row)"
        return cell ?? UITableViewCell()
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return letterArr[section]
    }
}
